package com.btpj.module_search.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.paging.PagedListAdapter
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import com.btpj.jetpack.entity.DataBean
import com.btpj.module_search.R
import com.btpj.module_search.databinding.ListItemSearchResultBinding

class SearchResultListAdapter(private val context: Context) :
    PagedListAdapter<DataBean, SearchResultListAdapter.ViewHolder>(object :
        DiffUtil.ItemCallback<DataBean>() {
        override fun areItemsTheSame(oldItem: DataBean, newItem: DataBean): Boolean {
            return oldItem.id == newItem.id
        }

        override fun areContentsTheSame(oldItem: DataBean, newItem: DataBean): Boolean {
            return oldItem == newItem
        }
    }) {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val binding = DataBindingUtil.inflate<ListItemSearchResultBinding>(
            LayoutInflater.from(context),
            R.layout.list_item_search_result, parent, false
        )
        return ViewHolder(binding.root, binding)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.setData(getItem(position))
    }

    class ViewHolder(
        itemView: View,
        private val binding: ListItemSearchResultBinding
    ) : RecyclerView.ViewHolder(itemView) {

        fun setData(item: DataBean?) {
            binding.data = item
        }

    }
}